
/**********************************************************************
 *
 * Copyright Apple Computer, Inc. 1986
 * All Rights Reserved
 *
 *	Data			: This program reads the code segment 1 from
 *						the Resource file and and saves it, stripping.
 *						off code segment 0.
 *
 *	Author			: xxxxxx xxxxxx, July 30, 1986.
 *
 *	Mod History		: Nil.
 *
 **********************************************************************/
						

#include <stdio.h>
#include <types.h>
#include <osutils.h>
#include <files.h>
#include <resources.h>
#include <memory.h>
#include <errno.h>



pascal void _CalcCRC (SizeCode,CodePtr,crc)
long		SizeCode;
Ptr			CodePtr;
long		*crc;
extern;

/******************************************************************
	Main
*******************************************************************/
main(argc,argv)

int	  argc;
char  *argv[];

{
  short		refnum;
  Handle	CodeHandle;		/* Handle to code resource */
  short		IOR;			/* IO Result */
  long		SizeCode;		/* Size of the code */
  Ptr		CodePtr;		/* Pointer to the code */
  long		crc;			/* the crc value */
  int		FileDescript;	/* The file descriptor of the data file */
  unsigned	NumBWritten;	/* Number of bytes written per write */
  unsigned	Total;			/* Total number of bytes written */
		
  if (argc == 1)
    {
      fprintf(stderr,"### ERROR : No files specified.\n");
      fprintf(stderr,"### SYNTAX: Data  rsrcFileName  DataFileName\n");
      fprintf(stderr,"### DSCRPT: Copy code segment 1 to a data file.\n");
    }
  else if (argc != 3)
    {
      fprintf(stderr,"### ERROR : Wrong number of parameters specified.\n");
      fprintf(stderr,"### SYNTAX: Data  rsrcFileName  DataFileName\n");
      fprintf(stderr,"### DSCRPT: Copy code segment 1 to a data file.\n");
    }
  else
    {
      refnum = OpenResFile(argv[1]);
	  if (refnum < 0 )
        fprintf(stderr,"### ERROR : Resource file: %s can't be opened. err = %d.\n",argv[1],refnum);
	  else
	    {
		   CodeHandle = GetResource('CODE',1);
	       HLock(CodeHandle);
	       IOR = ResError();
           if (IOR != 0)
             fprintf(stderr,"### ERROR : Code resource not available. Err = %d\n",errno);
		   else
		     {
		       SizeCode = GetHandleSize(CodeHandle);
		       SizeCode = SizeCode - 4;										/* Skip first 4 bytes (Resource header) */
		       CodePtr = (Ptr) ( ((long) *CodeHandle) & 0x0FFFFFF) + 4;		/* Skip first 4 bytes (Resource header) */
		
			   FileDescript = creat(argv[2]);
			   if (FileDescript < 0)
                 fprintf(stderr,"### ERROR : Data file: %s can't be opened. err = %d.\n",argv[2],errno);
			   else
			     {
				   Total = 0;
				   while (Total < SizeCode)
				     {
					   NumBWritten = write(FileDescript,CodePtr,SizeCode);
					   if (NumBWritten < 0)
					     {
						   fprintf(stderr,"### ERROR : Write err = %d.\n",errno);
						   Total = SizeCode;
						 }
					   else
					     Total = Total + NumBWritten;
					 }
				   close(FileDescript);
				 }

			   CloseResFile(refnum);
			 }
	    }
	}
}

